﻿/* 
* Copyright (c) 2010, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Windows;
using System.Windows.Media;
using System.Xml;
using System.Xml.Serialization;
using Bio.Views.Structure.Editor.Internal.Rendering;

namespace Bio.Views.Structure.Editor.Internal.Options
{
    public class NucleotideVisualRenderingOptions : IXmlSerializable
    {
        public FontFamily NucleotideFontFamily { get; set; }
        public FontWeight NucleotideFontWeight { get; set; }
        public FontStyle NucleotideFontStyle { get; set; }
        public NucleotideRenderingMode NucleotideRenderingStyle { get; set; }
        public Brush NucleotideFill { get; set; }
        public double NucleotideFontSize { get; set; }
        public char NucleotideSubstitution { get; set; }
        public bool IsNucleotideSubstituted { get; set; }

        public double NucleotideCircleDiameter { get; set; }
        public double NucleotideCircleEdgeThickness { get; set; }
        public bool NucleotideCircleIsFilled { get; set; }
        public Brush NucleotideCircleFill { get; set; }
        public Brush NucleotideCircleStroke { get; set; }

        public NucleotideVisualRenderingOptions(NucleotideVisualRenderingOptions toCopy)
        {
            NucleotideFontFamily = toCopy.NucleotideFontFamily;
            NucleotideFontWeight = toCopy.NucleotideFontWeight;
            NucleotideFontStyle = toCopy.NucleotideFontStyle;
            NucleotideFontSize = toCopy.NucleotideFontSize;
            NucleotideRenderingStyle = toCopy.NucleotideRenderingStyle;
            NucleotideFill = toCopy.NucleotideFill.CloneCurrentValue();
            IsNucleotideSubstituted = toCopy.IsNucleotideSubstituted;
            NucleotideSubstitution = toCopy.NucleotideSubstitution;
            NucleotideCircleDiameter = toCopy.NucleotideCircleDiameter;
            NucleotideCircleEdgeThickness = toCopy.NucleotideCircleEdgeThickness;
            NucleotideCircleIsFilled = toCopy.NucleotideCircleIsFilled;
            NucleotideCircleFill = toCopy.NucleotideCircleFill.CloneCurrentValue();
            NucleotideCircleStroke = toCopy.NucleotideCircleStroke.CloneCurrentValue();
        }

        public NucleotideVisualRenderingOptions()
        {
            NucleotideFontFamily = new FontFamily("Arial");
            NucleotideFontWeight = FontWeights.Regular;
            NucleotideFontStyle = FontStyles.Normal;
            NucleotideFontSize = 8.0;
            NucleotideRenderingStyle = NucleotideRenderingMode.Text;
            NucleotideFill = new SolidColorBrush(Colors.Black);
            IsNucleotideSubstituted = false;
            NucleotideSubstitution = ' ';
            NucleotideCircleDiameter = 2.0;
            NucleotideCircleEdgeThickness = 0.0;
            NucleotideCircleIsFilled = true;
            NucleotideCircleFill = new SolidColorBrush(Colors.Black);
            NucleotideCircleStroke = new SolidColorBrush(Colors.Black);
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            if (reader.MoveToAttribute("NucleotideFontFamily") && !string.IsNullOrEmpty(reader.Value))
            {
                FontFamilyConverter converter = new FontFamilyConverter();
                try
                {
                    NucleotideFontFamily = (FontFamily)converter.ConvertFromString(reader.Value);
                }
                catch { }
            }

            if(reader.MoveToAttribute("NucleotideFontWeight") && !string.IsNullOrEmpty(reader.Value))
            {
                FontWeightConverter converter = new FontWeightConverter();
                try
                {
                    NucleotideFontWeight = (FontWeight)converter.ConvertFromString(reader.Value);
                }
                catch { }
            }

            if (reader.MoveToAttribute("NucleotideFontStyle") && !string.IsNullOrEmpty(reader.Value))
            {
                FontStyleConverter converter = new FontStyleConverter();
                try
                {
                    NucleotideFontStyle = (FontStyle)converter.ConvertFromString(reader.Value);
                }
                catch { }
            }

            if (reader.MoveToAttribute("NucleotideFontSize") && !string.IsNullOrEmpty(reader.Value))
            {
                double val;
                if (double.TryParse(reader.Value, out val))
                    NucleotideFontSize = val;
            }

            if (reader.MoveToAttribute("NucleotideRenderingStyle") && !string.IsNullOrEmpty(reader.Value))
            {
                NucleotideRenderingMode val;
                if (Enum.TryParse<NucleotideRenderingMode>(reader.Value, out val))
                    NucleotideRenderingStyle = val;
            }

            BrushConverter brushConverter = new BrushConverter();
            if (reader.MoveToAttribute("NucleotideFill") && !string.IsNullOrEmpty(reader.Value))
            {
                
                try
                {
                    NucleotideFill = (Brush)brushConverter.ConvertFromString(reader.Value);
                }
                catch { }
            }

            if (reader.MoveToAttribute("IsNucleotideSubstituted") && !string.IsNullOrEmpty(reader.Value))
            {
                bool val;
                if (bool.TryParse(reader.Value, out val))
                    IsNucleotideSubstituted = val;
            }

            if (reader.MoveToAttribute("NucleotideSubstitution") && !string.IsNullOrEmpty(reader.Value))
            {
                char val;
                if (char.TryParse(reader.Value, out val))
                    NucleotideSubstitution = val;
            }

            if (reader.MoveToAttribute("NucleotideCircleDiameter") && !string.IsNullOrEmpty(reader.Value))
            {
                double val;
                if (double.TryParse(reader.Value, out val))
                    NucleotideCircleDiameter = val;
            }

            if (reader.MoveToAttribute("NucleotideCircleEdgeThickness") && !string.IsNullOrEmpty(reader.Value))
            {
                double val;
                if (double.TryParse(reader.Value, out val))
                    NucleotideCircleEdgeThickness = val;
            }

            if (reader.MoveToAttribute("NucleotideCircleIsFilled") && !string.IsNullOrEmpty(reader.Value))
            {
                bool val;
                if (bool.TryParse(reader.Value, out val))
                    NucleotideCircleIsFilled = val;
            }

            if (reader.MoveToAttribute("NucleotideCircleFill") && !string.IsNullOrEmpty(reader.Value))
            {
                NucleotideCircleFill = (Brush)brushConverter.ConvertFromString(reader.Value);
            }

            if (reader.MoveToAttribute("NucleotideCircleStroke") && !string.IsNullOrEmpty(reader.Value))
            {
                NucleotideCircleStroke = (Brush)brushConverter.ConvertFromString(reader.Value);
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            if(NucleotideFontFamily != null)
                writer.WriteAttributeString("NucleotideFontFamily", NucleotideFontFamily.ToString());

            writer.WriteAttributeString("NucleotideFontWeight", NucleotideFontWeight.ToString());
            writer.WriteAttributeString("NucleotideFontStyle", NucleotideFontStyle.ToString());
            writer.WriteAttributeString("NucleotideFontSize", NucleotideFontSize.ToString());
            writer.WriteAttributeString("NucleotideRenderingStyle", NucleotideRenderingStyle.ToString());
            
            if(NucleotideFill != null)
                writer.WriteAttributeString("NucleotideFill", NucleotideFill.ToString());

            writer.WriteAttributeString("IsNucleotideSubstituted", IsNucleotideSubstituted.ToString());
            writer.WriteAttributeString("NucleotideSubstitution", NucleotideSubstitution.ToString());
            writer.WriteAttributeString("NucleotideCircleDiameter", NucleotideCircleDiameter.ToString());
            writer.WriteAttributeString("NucleotideCircleEdgeThickness", NucleotideCircleEdgeThickness.ToString());
            writer.WriteAttributeString("NucleotideCircleIsFilled", NucleotideCircleIsFilled.ToString());
            
            if(NucleotideCircleFill != null)
                writer.WriteAttributeString("NucleotideCircleFill", NucleotideCircleFill.ToString());

            if(NucleotideCircleStroke != null)
                writer.WriteAttributeString("NucleotideCircleStroke", NucleotideCircleStroke.ToString());
        }

        #endregion
    }
}
